import Vue from 'vue'
import app from './App.vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router.js'

import VuePreview from 'vue-preview'
Vue.use(VuePreview)

import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root='http://vue.studyit.io'
Vue.http.options.emulateJSON = true





// import {Header,Swip,SwipItem,Button,Lazyload} from 'mint-ui'
// Vue.component(Header.name,Header)
// vue.component(Swip.name,Swip)
// Vue.component(SwipItem.name,SwipItem,Button)
// Vue.use(Lazyload)
import MintUI from 'mint-ui' 
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

import moment from 'moment'
Vue.filter('dateFormat',function(dataStr,pattern="YYYY-MM-DD HH:mm:ss"){
   return moment(dataStr).format(pattern)

})

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
import setPrototypeOf from '_setprototypeof@1.1.1@setprototypeof'

import Vuex from 'vuex'
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem('car') || '[]')

var store = new Vuex.Store({
    state:{
        car: car,
    },
    mutations:{
       addToCar(state,goodsinfo){

        var flag = false
          state.car.some( item => {
              if (item.id == goodsinfo.id){
                  item.count += goodsinfo.count
                  flag = true
                  return true
              }
          })
          if(!flag){
            state.car.push(goodsinfo)
        }
        localStorage.setItem('car',JSON.stringify(state.car))
         
       },

       updataGoodsInfo(state , goodsinfo){
           state.car.some( item => {
               if (item.id == goodsinfo.id){
                   item.count = parseInt(goodsinfo.count)
               }
               return true
           })
          localStorage.setItem('car',JSON.stringify(state.car))
       },

       remove(state,id){
           state.car.some( item , i => {
               if(item.id == id){
                   state.car.splice('i',1)
                   return true
               }
              
           })
           localStorage.setItem('car',JSON,stringify(state.car))
       },
       updataSelectedChanged(state,info){
           state.car,forEach( item => {
               if (item.id == info.id){
                   item.selected = info.selected
                   return true
               }
           })
           localStorage.setItem('car',JSON.stringify(state.car))
       },

    },
    getters:{
        getAllCount(state){
            var c = 0
            state.car.forEach( item => {
                c += item.count
            })
            return c
        }

    },
    getGoodsCount(){
        var o = {}
        state.car.forEach( item => {
            o[item.id] = item.count
        })
        return o
    },

    getGoodsSelected(){
        var o = {}
        state.car.forEach( item =>{
            o[item.id] = item.selected
        })
        return o
    },

    getGoodsCountAndAmount(state){
        var o = {count:0,amount:0}
        state.car.forEach( item => {
            if (item.selected){
                o.count += item.count
                o.amount += item.price*item.count
            }
           
        })
        return o
    },
})

var vm = new Vue({
    el:'#app',
    render: c => c(app),
    router,
    store
})